package com.mooc.ppjoke.ui.my_view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.databinding.BindingAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.mooc.lib_common.PixUtils;

import org.jetbrains.annotations.NotNull;

import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * @ClassName: NetImageView$
 * @Description: java类作用描述
 * @Author: 魏靳元
 * @CreateDate: 2022/8/17 10:25
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class NetImageView extends AppCompatImageView {
    public NetImageView(@NonNull @NotNull Context context) {
        super(context);
    }

    public NetImageView(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public NetImageView(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void bindData(int widthPx,int heightPx,int marginLeft,String imgUrl){
        bindData(widthPx, heightPx, marginLeft, PixUtils.getScreenWidth(), PixUtils.getScreenHeight(), imgUrl);
    }


    public void bindData(int widthPx,int heightPx,int marginLeft,int maxWidth,int maxHeight,String imgUrl){
        if (TextUtils.isEmpty(imgUrl)) {
            setVisibility(GONE);
            return;
        } else {
            setVisibility(VISIBLE);
        }
        if(widthPx<=0||heightPx<=0){
            Glide.with(this).load(imgUrl).into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull @NotNull Drawable resource, @Nullable @org.jetbrains.annotations.Nullable Transition<? super Drawable> transition) {
                    int height = resource.getIntrinsicHeight();
                    int width = resource.getIntrinsicWidth();
                    setSize(width,height,marginLeft,maxWidth,maxHeight);
                    setImageDrawable(resource);
                }
            });
        }else {
            setSize(widthPx,heightPx,marginLeft,maxWidth,maxHeight);
            setImgUrl(this, imgUrl, false);
        }

    }

    private void setSize(int width, int height, int marginLeft, int maxWidth, int maxHeight) {
        int finalWidth,finalHeight;
        if(width>height){
            finalWidth=maxWidth;
            finalHeight= (int) (height/(width*1.0/finalWidth));
        }else {
            finalHeight=maxHeight;
            finalWidth= (int) (width/(height*1.0/finalHeight));
        }
        ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(finalWidth, finalHeight);
        marginLayoutParams.leftMargin=height>width? PixUtils.dp2px(marginLeft) :0;
        setLayoutParams(marginLayoutParams);
    }
    public void setImageUrl(String imageUrl) {
        setImgUrl(this, imageUrl, false);
    }

    @BindingAdapter(value = {"image_url", "isCircle"})
    public static void setImageUrl(NetImageView view, String imageUrl, boolean isCircle) {
        view.setImgUrl(view, imageUrl, isCircle, 0);
    }
    @BindingAdapter(value = {"image_url", "isCircle", "radius"}, requireAll = false)
    public static void setImgUrl(NetImageView view, String imageUrl, boolean isCircle, int radius) {
        RequestBuilder<Drawable> builder = Glide.with(view).load(imageUrl);
        if (isCircle) {
            builder.transform(new CircleCrop());
        } else if (radius > 0) {
            builder.transform(new RoundedCornersTransformation(PixUtils.dp2px(radius), 0));
        }
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams != null && layoutParams.width > 0 && layoutParams.height > 0) {
            builder.override(layoutParams.width, layoutParams.height);
        }
        builder.into(view);
    }
    //    requireAll = true  -->两个参数都要写才可以，false -->两个参数写一个就行
    @BindingAdapter(value = {"image_url","isCircle"},requireAll = true)
    public static void setImgUrl(NetImageView view,String url,boolean isCircle){
        RequestBuilder<Drawable> builder = Glide.with(view).load(url);
        if(isCircle){
            builder.transform(new CircleCrop());
        }
        ViewGroup.LayoutParams layoutParams=view.getLayoutParams();
        // 避免资源浪费（将设置好的宽高给网络加载的图片宽高进行设置）
        if(layoutParams!=null && layoutParams.width>0 &&layoutParams.height>0){
            builder.override(layoutParams.width,layoutParams.height);
        }
        builder.into(view);
    }

    @BindingAdapter(value = {"blur_url", "radius"})
    public static void setBlurImageUrl(NetImageView imageView,String coverUrl, int radius) {
        Glide.with(imageView).load(coverUrl).override(radius)
                .transform(new BlurTransformation())
                .dontAnimate()
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull @NotNull Drawable resource, @Nullable @org.jetbrains.annotations.Nullable Transition<? super Drawable> transition) {
                        imageView.setBackground(resource);
                    }
                });
    }
}

